import { createStore, applyMiddleware, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import { reducer as homeReducer } from '../containers/Home/store'
import { reducer as headerReducer } from '../components/Header/store'
import { reducer as translationReducer } from '../containers/Translation/store'

import clientAxios from '../client/request'
import createServerAxios from '../server/request'

const reducer = combineReducers({
  home: homeReducer,
  header: headerReducer,
  translation: translationReducer
})

// 之前返回单个 store 的方式
// let preloadedState = {}

// if(typeof window !== 'undefined'){
//   preloadedState = window.context && window.context.state
// }

// export default createStore(reducer, preloadedState, applyMiddleware(thunk))

// 讲师的方式
export const getServerStore = (req) => {
  return createStore(reducer, applyMiddleware(thunk.withExtraArgument(createServerAxios(req))))
}

export const getClientStore = () => {
  const preloadedState = window.context.state
  return createStore(reducer, preloadedState, applyMiddleware(thunk.withExtraArgument(clientAxios)))
}
